<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>面向对象 | Oliver知识收集站</title>
    <meta name="generator" content="VuePress 1.9.7">
    
    <meta name="description" content="享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css" as="style"><link rel="preload" href="/oliver-vuepress/assets/js/app.c21e6ffc.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/3.6dd9a2a1.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/1.898920d0.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/22.da4bc7f7.js" as="script"><link rel="prefetch" href="/oliver-vuepress/assets/js/10.41b2bf91.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/11.a95c117d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/12.8607f0e1.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/13.a52d6846.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/14.249b4e52.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/15.d458d12e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/16.ba334206.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/17.1b91c9fa.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/18.e2ea2eb5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/19.bf0e2553.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/20.268bd174.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/21.cd1bbed5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/23.12f0c72f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/24.b7886742.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/25.6e71af85.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/26.5c127243.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/27.e98fd8bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/28.ce83b09c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/29.50398f0f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/30.05e1339c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/31.ef4b13fb.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/32.ba5f8351.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/33.3902db0a.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/34.36a05884.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/35.87215872.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/36.db360c58.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/37.402e5374.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/38.c9228dd8.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/39.72ba5d1f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/4.7bb03d47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/40.7e7949bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/41.c0d5b947.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/42.d9984467.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/43.e6a43668.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/44.10d7fe47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/45.f692ec2d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/46.9b920343.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/47.8e3d94f9.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/48.7d356e5b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/49.b0df6271.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/5.1fa544da.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/50.805e1466.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/51.1b31d40e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/52.44e69a41.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/53.da1def53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/54.6569f7db.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/55.5fc3de47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/56.da649377.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/57.6ff15ed4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/58.a62f6424.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/59.f68ae517.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/6.f5bd8e9b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/60.dda416bc.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/61.4e0c719f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/62.8c5ef01e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/63.7089eb8b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/64.b5ec150d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/65.6720cda4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/66.4ee90e29.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/67.cc4b0c6d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/7.d5950c53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/8.382fb3a5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/9.d593f4c1.js">
    <link rel="stylesheet" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-130b300a><div data-v-130b300a><div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-130b300a data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Oliver知识收集站</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div class="hide" data-v-130b300a><header class="navbar" data-v-130b300a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/oliver-vuepress/" class="home-link router-link-active"><!----> <span class="site-name">Oliver知识收集站</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link router-link-active"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-130b300a></div> <aside class="sidebar" data-v-130b300a><div class="personal-info-wrapper" data-v-39576ba9 data-v-130b300a><!----> <h3 class="name" data-v-39576ba9>
    oliver.shi
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>52</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>6</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link router-link-active"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>基础篇</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/oliver-vuepress/articles/java/basics/oop.html" aria-current="page" class="active sidebar-link">面向对象</a></li><li><a href="/oliver-vuepress/articles/java/basics/enum.html" class="sidebar-link">枚举</a></li><li><a href="/oliver-vuepress/articles/java/basics/datatype.html" class="sidebar-link">数据类型</a></li><li><a href="/oliver-vuepress/articles/java/basics/string.html" class="sidebar-link">字符串</a></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>面向对象</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div data-v-130b300a><main class="page"><section><div class="page-title"><h1 class="title">面向对象</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>oliver.shi</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>11/7/2020</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>Java</span></i></div></div> <div class="theme-reco-content content__default"><h2 id="什么是面向对象"><a href="#什么是面向对象" class="header-anchor">#</a> 什么是面向对象</h2> <h3 id="什么是面向过程"><a href="#什么是面向过程" class="header-anchor">#</a> 什么是面向过程 ?</h3> <blockquote><p>自顶而下的编程模式</p></blockquote> <p>就是说：将一个功能点 一步接一步的调用函数实现，并没后做相对应的抽象</p> <h3 id="什么是面向对象-2"><a href="#什么是面向对象-2" class="header-anchor">#</a> 什么是面向对象？</h3> <blockquote><p>将事务高度抽象化的编程模式</p></blockquote> <p>就是说：将一个功能点中的每个步进行相对应的抽象形成不同的对象，通过不同对象之间的调用，或组合来完成功能</p> <h3 id="面向对象的三大特征"><a href="#面向对象的三大特征" class="header-anchor">#</a> 面向对象的三大特征</h3> <ul><li>封装</li> <li>继承</li> <li>多态</li></ul> <blockquote><p>具体含义可自行百度</p></blockquote> <h3 id="面向对象的五大基本原则"><a href="#面向对象的五大基本原则" class="header-anchor">#</a> 面向对象的五大基本原则：</h3> <h4 id="单一职责原则-single-responsibility-principle"><a href="#单一职责原则-single-responsibility-principle" class="header-anchor">#</a> 单一职责原则（Single-Responsibility Principle）</h4> <ul><li>核心思想：一个类，最好只做一件事情  并且 仅有一个引起它变化的原因</li> <li>简单的理解 ：一个对象的功能要单一，可以理解为 低耦合、高内聚</li></ul> <h4 id="开放封闭原则-open-closed-principle"><a href="#开放封闭原则-open-closed-principle" class="header-anchor">#</a> 开放封闭原则（Open-Closed principle）</h4> <ul><li>核心思想: 软件实体应该是可扩展，而不可修改的
<ul><li>对于扩展是开放的</li> <li>对于修改是封闭的</li></ul></li> <li>扩展开放：
<ul><li>因为需求总是在变化的，我们可以对现有代码进行扩展，在变化时我们可以通过继承或多态机制，来改变固有行为，实现新的扩展方法</li></ul></li> <li>修改封闭：
<ul><li>类一但时设计完成，就可以独立完成其工作，而不要对其进行任何尝试的修改。</li> <li>核心思想就是对抽象编程，而不对具体编程，因为抽象相对稳定。让类依赖于固定的抽象，所以修改就是封闭的</li></ul></li></ul> <h4 id="liskov替换原则-liskov-substitution-principle"><a href="#liskov替换原则-liskov-substitution-principle" class="header-anchor">#</a> Liskov替换原则（Liskov-Substitution Principle）</h4> <ul><li>子类型必须能够替换掉它们的父类型。简单地说，这是因为子类型继承了父类，所以子类可以以父类的身份出现。</li></ul> <h4 id="依赖倒置原则-dependecy-inversion-principle"><a href="#依赖倒置原则-dependecy-inversion-principle" class="header-anchor">#</a> 依赖倒置原则（Dependecy-Inversion Principle）</h4> <ul><li>核心思想：抽象不应该依赖写，细节应该依赖于抽象</li> <li>简单的理解：我们要针对接口编程，而不要针对实现编程
<ul><li>高层模块不应该依赖底层模块，两个都应该依赖抽象
<ul><li>缺点：高度模块太依赖底层模块，耦合太紧密。底层模块发送变化会影响到高层模块</li></ul></li> <li>抽象不用管依赖具体（细节），具体 （细节） 应该依赖抽象
<ul><li>有点：修改底层不会影响到高层模块，减小了他们之间的耦合度，增强系统的稳定性</li></ul></li></ul></li></ul> <h4 id="接口隔离原则-interface-segregation-principle"><a href="#接口隔离原则-interface-segregation-principle" class="header-anchor">#</a> 接口隔离原则（Interface-Segregation Principle）</h4> <ul><li>核心思想：使用多个小的专门的接口，而不要使用一个大的总接口</li> <li>原则体现：接口应该是内聚的，应该避免 <code>胖</code>接口。胖接口可能有很多我们用不到的方法，不要强迫依赖不用的方法，这是一种接口污染</li> <li>通俗的理解：不要强迫客户使用他们不想用的商品</li> <li>解决方案：
<ul><li>委任分离：通过增加一个新的类型来委托客户的请求，隔离客户和接口的直接依赖，但是会增加系统的开销</li> <li>多重继承分离接口：通过集成多个单一小接口，来解决继承的大接口</li></ul></li></ul> <h2 id="封装、继承、多态"><a href="#封装、继承、多态" class="header-anchor">#</a> 封装、继承、多态</h2> <h3 id="什么是多态"><a href="#什么是多态" class="header-anchor">#</a> 什么是多态</h3> <blockquote><p>同一操作作用于不同的对象，可以有不同的解释，产生不同的执行结果</p> <p>多态应该是一种运行期的状态</p></blockquote> <p>多态的必要条件：</p> <ul><li>类要有继承 或者接口实现</li> <li>子类要重写父类的方法</li> <li>父类的引用执行子类的对象</li></ul> <h2 id="方法重写-与-重载"><a href="#方法重写-与-重载" class="header-anchor">#</a> 方法重写 与 重载</h2> <h3 id="重写"><a href="#重写" class="header-anchor">#</a> 重写</h3> <br> <img src="/oliver-vuepress/java/oop/overriding.png" alt="foo"> <h3 id="重载"><a href="#重载" class="header-anchor">#</a> 重载</h3> <br> <img src="/oliver-vuepress/java/oop/overloading.png" alt="foo"> <blockquote><p>其他相关：</p> <p>https://blog.csdn.net/carson_ho/article/details/81502540</p> <p>https://www.cnblogs.com/constantwang/p/11141566.html</p></blockquote> <h3 id="java的继承-与-实现"><a href="#java的继承-与-实现" class="header-anchor">#</a> Java的继承 与 实现</h3> <blockquote><p>Java 中</p> <p>继承的根本原因是要 <code>复用</code></p> <p>实现的根本原因是要 <code>定义一个标准</code></p></blockquote> <p>继承使用 <code>extends</code> 关键字实现</p> <p>实现使用 <code>implemenets</code> 实现</p> <h2 id="java的构造函数-与-默认构造函数"><a href="#java的构造函数-与-默认构造函数" class="header-anchor">#</a> Java的构造函数 与 默认构造函数</h2> <p>构造函数：主要用来在创建对象时初始化对象，一般会跟new 运算符一起使用，给对象成员变量赋初始值</p> <p>构造函数的特点：</p> <ul><li>构造函数的 <em>名称必须与类名相同</em>， 并且大小写铭感</li> <li>构造函数 没有返回值、也不能用 void修饰 （如果构造函数 加上了返回值，就会变为普通函数）</li> <li>一个类可以用有多个构造方法，如果没有定义构造方法，编译器会自动创建一个无参数且方法体为空的默认构造函数</li> <li>构造方法可以进行 <em>重载</em></li></ul> <p>显示声明的无惨构造函数 和 默认构造函数的区别</p> <ul><li>创建主体不同：
<ul><li>无惨构造函数是由：开发者创建的</li> <li>默认构造函数是由：编译器生成的</li></ul></li> <li>创建方式上不同:
<ul><li>开发者在类中显示声明无参构造函数时，编译器不会生产默认构造函数</li> <li>默认构造函数只能在类中没有显式声明构造函数的情况下，由编译器生成</li></ul></li> <li>创建目的上不同：
<ul><li>开发者在类中声明无参构造函数，视为了对象进行初始化操作</li> <li>编译器生成默认构造函数，是为了在 JVM进行类加载时，能够顺利验证父类的数据信息</li></ul></li></ul> <p>类中 构造函数的执行顺序：</p> <p>会先执行实例化成员变量的初始化表达式，然后在调用自己的构造函数</p> <blockquote><p>其他知识点：</p> <p>Java对象的创建过程 &amp; 构造函数的创建过程： https://segmentfault.com/a/1190000021944895</p></blockquote> <h2 id="类变量、成员变量、局部变量"><a href="#类变量、成员变量、局部变量" class="header-anchor">#</a> 类变量、成员变量、局部变量</h2> <br> <img src="/oliver-vuepress/java/oop/variable.png" alt="foo"></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">4/21/2022, 8:17:59 AM</span></div></footer> <div class="page-nav"><p class="inner"><!----> <span class="next"><a href="/oliver-vuepress/articles/java/basics/enum.html">
            枚举
          </a></span></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/oop.html#什么是面向对象" class="sidebar-link reco-side-什么是面向对象" data-v-cb1513f6>什么是面向对象</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/oop.html#什么是面向过程" class="sidebar-link reco-side-什么是面向过程" data-v-cb1513f6>什么是面向过程 ?</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/oop.html#什么是面向对象-2" class="sidebar-link reco-side-什么是面向对象-2" data-v-cb1513f6>什么是面向对象？</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/oop.html#面向对象的三大特征" class="sidebar-link reco-side-面向对象的三大特征" data-v-cb1513f6>面向对象的三大特征</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/oop.html#面向对象的五大基本原则" class="sidebar-link reco-side-面向对象的五大基本原则" data-v-cb1513f6>面向对象的五大基本原则：</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/oop.html#封装、继承、多态" class="sidebar-link reco-side-封装、继承、多态" data-v-cb1513f6>封装、继承、多态</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/oop.html#什么是多态" class="sidebar-link reco-side-什么是多态" data-v-cb1513f6>什么是多态</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/oop.html#方法重写-与-重载" class="sidebar-link reco-side-方法重写-与-重载" data-v-cb1513f6>方法重写 与 重载</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/oop.html#重写" class="sidebar-link reco-side-重写" data-v-cb1513f6>重写</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/oop.html#重载" class="sidebar-link reco-side-重载" data-v-cb1513f6>重载</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/oop.html#java的继承-与-实现" class="sidebar-link reco-side-java的继承-与-实现" data-v-cb1513f6>Java的继承 与 实现</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/oop.html#java的构造函数-与-默认构造函数" class="sidebar-link reco-side-java的构造函数-与-默认构造函数" data-v-cb1513f6>Java的构造函数 与 默认构造函数</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/java/basics/oop.html#类变量、成员变量、局部变量" class="sidebar-link reco-side-类变量、成员变量、局部变量" data-v-cb1513f6>类变量、成员变量、局部变量</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/oliver-vuepress/assets/js/app.c21e6ffc.js" defer></script><script src="/oliver-vuepress/assets/js/3.6dd9a2a1.js" defer></script><script src="/oliver-vuepress/assets/js/1.898920d0.js" defer></script><script src="/oliver-vuepress/assets/js/22.da4bc7f7.js" defer></script>
  </body>
</html>
